"use strict";


//  get user state from user table 
//  if need to login : logn 
//   start step action 

// action ste p:    set query, set cstlmgr info  with prev state . 
//  check which step : substep 
// do cur step  
function CSpActionMgr() {
	this.objName = "CSpActionMgr: ";
	this.initialize();
};


CSpActionMgr.prototype = {
	
	// get user state : ?? 
	initialize: function() {
		this.u_idx = -1;
	},
	
 
/*
	doAction:: function() {
		this.checkUserState()
	},
*/
	/*
	makeAllView: function() {
				var i = 0;
				createUserView( i ) ;
				
				function createUserView( idx ) {
												   // check end of idx 
						if ( idx >=   em.spusermgr.userCnt() )  {
								debug.log( this, 'End of Arr User States .', idx );
								return;		
						}
						
						var new_u_s = em.spusermgr.getUserStates( idx ); 
						debug.log(this,  '   * nane :  ', new_u_s.name, new_u_s , ' idx:', this.u_idx );
						// check need to login 
							if ( ! new_u_s.started ) {
									// create user view 
								new_u_s.view = new CSP30View( new_u_s.name );
					
								self.switchUserCtx( new_u_s );
								
								// make new sp action obj 
								new_u_s.sps = new CSP30Action();
	
								// cb when cstl move done 
								em.cbs.userchanged.add( self.cb_userchanged = function( ){
										em.cbs.userchanged.remove( self.cb_userchanged );
										debug.log(self, 'this', this, 'cb_cstlmove in start SP GM  '); 
										
										new_u_s.started = true;
										
										// store cur states 
										em.spusermgr.setUserStates (  em.cstlmgr.getAllStates(new_u_s) );
										
										// make next user view & update state. 
										createUserView( idx++  );
										return;
									}  );
									
								// do login :    async : go next 
								em.accmgr.startGM( new_u_s.name);
	
							}
				};
		},*/
	
		// entry ::: do...
	// get user state 
	doAction: function() {
		var self = this;
		debug.log(this,  ' doAction ========== ');

		var dfd =  $.Deferred();
		
		// get next user
		
		if ( ++this.u_idx  >= em.spusermgr.userCnt() ) {
			debug.log( this, ' Wrap all Users ', this.u_idx);
			this.u_idx = 0;
		}
		
		// checkUserState
		// get state
		var new_u_s = em.spusermgr.getUserStates( this.u_idx ); 
		debug.log(this,  '   * nane :  ', new_u_s.name, new_u_s , ' idx:', this.u_idx );
		



/*
		// make us at once 
		if ( ! this.initAll ) {
			 // 
			 							return dfd.promise();
		}*/

		// check need to login 
		if ( ! new_u_s.started && new_u_s.exist == true) {
			
				// create user view 
			new_u_s.view = new CSP30View( new_u_s.name );

			self.switchUserCtx( new_u_s );
		
			// make new sp action obj 
			new_u_s.sps = new CSP30Action();
			
			// if (self.cb_userchanged  )
					// em.cbs.userchanged.remove( self.cb_userchanged );
								// cb when cstl move done 
			em.cbs.userchanged.add( self.cb_userchanged = function( ret ){
					// fail 
					if ( ret == -1 ) {	
						// go next user
						 dfd.reject( 1 );
						 new_u_s.exist = false;
						 return;
					}
					debug.log(self, 'this', new_u_s.name, ' :: cb fn:  before   ' ,  em.cbs.userchanged,  self.cb_userchanged); 
					em.cbs.userchanged.remove( self.cb_userchanged );
					debug.log(self, 'this', new_u_s.name, ' :: cb fn:   after ' , em.cbs.userchanged,   self.cb_userchanged); 
					debug.log(self, 'this', new_u_s.name, 'cb_cstlmove in start SP GM  '); 
					
					new_u_s.started = true;
					
					// store cur states 
					em.spusermgr.setUserStates (  em.cstlmgr.getAllStates(new_u_s) );
					
					// to start next right after..  
					dfd.reject( 1 );
				
				}  );
				
			// do login :    async : go next 
			em.accmgr.startGM( new_u_s.name);
			return dfd.promise();
		}

		this.switchUserCtx( new_u_s );
		
		
			//  lv > 30 : skip  :  precondition : setCurUser :: <- switch user .. fn 
			// after switch User ctx
		// disable lv check to donate !!!   
/*
		var userinfo = CDB.getCurUserInfo();
		if ( userinfo.grade >= 30 ){
					// noty  view 
					new_u_s.view.setFinishedState(true);
					
					dfd.resolve();
					return dfd.promise();
		}*/

		
		// start sp action 
		// this.startSPAction( new_u_s , dfd );
		new_u_s.sps.startSPAction(  dfd );
		
	// afte done.. call this.. 		
			// store cur states 
/*
				em.spusermgr.setUserStates ( this.u_idx, em.cstlmgr.getAllStates() );
		
		// ============		
				// switch  cur user.
	  	// em.spproxy.changeCurUser( new_u_states );
	  	
	  	// update view 
		// em.sp30view.updateUser( new_u_s );
		
		// start view refresh 
		em.sp30view.startRefresh();		*/

		
		return dfd.promise();
	},
	
	switchUserCtx: function( u_s ) {
				// connect view 
			// sp30 mode . info -> sp30 view. 
			em.infoview = u_s.view;
			em.cstlmgr.setView(u_s.view);
			u_s.view.current();
			
				// prepare context for cur user
				// set cur user : set cur uset to DB,  & get session key  & set Query : device,  server    
			em.accmgr.setCurUser(u_s.name);
				
			// set cur user 's cstlmgr info 
			em.cstlmgr.setNewUserInfo( u_s.castles );		
	},
	
	// stop seletec user sp action 
	stopUserAction: function( username) {
			var u_s = em.spusermgr.getUserStatesByName( username ); 
			debug.log(this,  '   * nane :  ', username , u_s.name , u_s );
		
			u_s.sps.stop (  u_s );
	},
	
	resumeUserAction: function( username) {
			var u_s = em.spusermgr.getUserStatesByName( username ); 
			debug.log(this,  '   * nane :  ', username , u_s.name , u_s );
		
			u_s.sps.resume (  u_s );
	},
/*
	startSPAction: function ( new_u_s ) {
		// check prev proceeded step
		
		// if no info
		if ( ! new_u_s.prev_step ) {
			// get system api  Qst info * // get study & blds
			em.tasks.getSPState( new_u_s.name , function(result) {
					
				// decision step : substep
					// test.. never 0 
					if ( result.data != 0 ) {
						// severe error !!
						alert ( 'TEST:::  ERROR in Get SP state ') ;
						return; 
					}
	
				// qst ... info ?? 
				if ( result.Qst[8] > 1 ) {
					new_u_s.step = 1;
					new_u_s.subs = 2;
					
				} else {
					new_u_s.step = 3;
				}
				
				if ( result.studies.scouting > 0 ) {
					new_u_s.step = 3; 
				}
				
				if ( result.blds.uni > 2 ) {
					new_u_s.step = 3;
				}
				
				// start 
				new_u_s.sps.startStep( new_u_s , step, subs);
				return;
			}); 
			 
			
		} else {
		// if there is info prev step 
		// start with cur step 
		// store finsined step nth  to new_u_s 
		new_u_s.sps.startStep( new_u_s, step, subs);
			
		}
			 
	},*/

	
	
	
};